The next topic is
request queuing, or how to avoid it in order
to achieve minimal latency. If a service does not
get request for a while, it scales down to
zero containers by stopping the last
remaining idle container. The first few requests that come in after your service scale to zero will queue while
the first container starts. You might know this
as cold start. Requests do not queue if there is a container available
to handle the request. In this example, you can
see how a Cloud Run service handles a request rate
that increases from zero. The first few requests will queue while the first
container is starting. This phase is marked by the blue highlighted
area on the chart. The latency penalty is proportional to the start
delay of your container. As you learned earlier, you can ignore container image download time as Cloud Run is optimized to materialize any
container image very fast. In the case of a cold start, the queuing time depends on the start time of
your application. You should also notice that as soon as there are
containers available, requests do not queue anymore. This is because Cloud
Run only assigns a request to an available
container if it is ready. A lot of applications
receive requests all the time and never
scale down to zero. However, even when
cold starts only happen when your container
has scaled to zero, you might not tolerate
the latencies. There is a setting that
helps avoid cold starts. With minimum instances, you can tell Cloud
Run to never remove the last container or even
more than one container. You can tell it to keep a container idle with the
minimum instances setting. You'll be charged for
keeping a container idle, but at a reduced rate. If incoming traffic is more than the existing
containers can handle, a new container or containers will be started
to help handle the load. Cloud Run uses
request-based autoscaling. This means it'll route a
maximum number of requests at the same time concurrently
to the same container. You can visualize this
with request slots. If all slots are in use, Cloud Run does not
send a new request to the container until
a slot is freed up. You can configure concurrency yourself if you know
what you're doing. Limit concurrency
to one if you have shared data or if
every request uses a predictable amount of
memory and you want to make sure your application does
not run out of memory. Containers that
have high CPU usage will not be overloaded
with more requests. There's a feedback loop
in Cloud Run which limits the maximum concurrency in case the CPU usage is too high. There is no such feedback
loop for memory pressure. To summarize, requests
will be queued temporarily if there are no containers or not
enough containers to handle the load. This allows time for
new containers to be started without losing
incoming requests. Cloud Run also keeps a copy of your container image in its internal storage to
start containers fast. This way, large container images load just as quickly as
tiny container images. As soon as your application
listens for TCP connections, Cloud Run decides
it's ready to handle web requests and starts to forward them as an
available container. You can configure the number of minimum instances to make sure at least one container
is available to handle a sudden
influx of requests. Otherwise, your number of containers may be scaled to zero during times when there is no requests load
increasing latency. Maximum concurrency is
the largest number of requests your
application containers can handle at a given time. This is configurable, and the default maximum is 250.